<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class count extends \plugin{
	//$type0infee_log,1tuifee_log,2手动补
	public function inall($item,$params=array(),$heparams=array()){
			$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
			$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
			$params['storeid']=$item['id'];
			$heparams['hestoreid']=$item['id'];
			//成交费
			$item['fee1']=$this->getoneyeji(0,$params,$heparams);
			//其他杂费
			$item['shouhoufee']=pdo_getsum('jy_yxw_feelog',$params,'shouhoufee');
			$item['peifee']=pdo_getsum('jy_yxw_feelog',$params,'peifee');
			$item['zhaofee']=pdo_getsum('jy_yxw_feelog',$params,'zhaofee');
			//定金费
			$item['fee2']=$this->getoneyeji(1,$params,$heparams);
			//分期缴费
			$item['fee3']=$this->getoneyeji(array(2,7),$params,$heparams);
			//退成交费
			$item['fee4']=$this->getoneyeji(3,$params,$heparams,$item['type']);
			//退定金费
			$item['fee5']=$this->getoneyeji(4,$params,$heparams,$item['type']);
			$item['tuiinfee']=$this->getoneyeji(6,$params,$heparams,$item['type']);
			//业绩：
			$item['yeji']=$item['fee1']+$item['fee2']+$item['fee3']+$item['tuiinfee']+$item['fee4']+$item['fee5'];
			$item['zufee']=price(($item['yeji']*16)/100,'int');
			$item['allfee']=price($item['yeji']+$item['zufee'],'int');
			//销售业绩
			$between=$params['jiao_time between']?:'';
			$item['now']=$this->getyeji($item,$between,$item['id']);
			//上月业绩
			$item['yes']=$this->getyeji($item,array($yesyue,$nowyue),$item['id']);
		return $item;
	}
	public function getyeji($item,$between=array(),$storeid=0,$is5=0){
		$params=array('uniacid'=>$_SESSION['uniacid']);
		if($between){
			$params['jiao_time between']=$between;
		}
		$heparams=$params;
		if($storeid){
			$params['storeid']=$storeid;
			$heparams['hestoreid']=$storeid;
		}elseif($is5){
			$params['worker5id']=$item['id'];
			$heparams['workerheid']=$item['id'];
		}else{
			$params['worker6id']=$item['id'];
			$heparams['workerheid']=$item['id'];
		}
		//进账
		$item['yesinall']=$this->getoneyeji(array(0,1,2,6,7),$params,$heparams,$item['type']);
		//退款
		$item['yesoutall']=$this->getoneyeji(array(3,4,5),$params,$heparams,$item['type']);
		$fee=price($item['yesinall']+$item['yesoutall'],'int');
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($item)."\n",FILE_APPEND);
		return $fee;
	}
	public function getoneyeji($type,$params=array(),$heparams=array(),$storetype=0){
		$params['type']=$heparams['type']=$type;
		if($params['storeid']==7){
			//print_r($params);exit;
		}
		if($type!=3 && $type!=4 && $type!=6){
			$storetype=0;
		}
		if($type!=6 && $type!=3){
			if($storetype==0 || $storetype==3){
				$infee=pdo_getsum('jy_yxw_feelog',$params,'fee')?:0;
				$hejian=pdo_getsum('jy_yxw_feelog',$params,'hefee')?:0;
				$hefee=pdo_getsum('jy_yxw_feelog',$heparams,'hefee')?:0;
				$fee=$infee+$hefee-$hejian;
			}elseif($storetype==1){
				$fee=pdo_getsum('jy_yxw_feelog',$params,'fee')?:0;
				$params['shoustoreid']=$params['storeid'];
				unset($params['storeid']);
				$shouhoufee=pdo_getsum('jy_yxw_feelog',$params,'shouhoufee')?:0;
				$fee +=$shouhoufee;
			}elseif($storetype==2){
				//总部
				unset($params['storeid']);
				$fee=pdo_getsum('jy_yxw_feelog',$params,'zufee');
			}
		}elseif($type==3){
			if($storetype==0 || $storetype==3){
				$fee=pdo_getsum('jy_yxw_feelog',$params,'zhaofee')?:0;
			}elseif($storetype==1){
				$params['shoustoreid']=$params['storeid'];
				unset($params['storeid']);
				$fee=pdo_getsum('jy_yxw_feelog',$params,'shouhoufee')?:0;
			}elseif($storetype==2){
				//总部
				unset($params['storeid']);
				$fee=pdo_getsum('jy_yxw_feelog',$params,'zufee');
			}
		}else{
			unset($params['type']);
			if($params['jiao_time between']){
				$params['tuidaytime between']=$params['jiao_time between'];
				unset($params['jiao_time between']);
			}
			if($params['jiao_time']){
				$params['tuidaytime']=$params['jiao_time'];
				unset($params['jiao_time']);
			}
			if($params['jiao_time >']){
				$params['tuidaytime >']=$params['jiao_time >'];
				unset($params['jiao_time >']);
			}
			if($params['jiao_time >=']){
				$params['tuidaytime >=']=$params['jiao_time >='];
				unset($params['jiao_time >=']);
			}
			$params['status']=4;
			if($storetype==0 || $storetype==3){
				$fee=pdo_getsum('jy_yxw_tuifee_log',$params,'infee3')?:0;
			}elseif($storetype==1){
				$params['shoustoreid']=$params['storeid'];
				$fee=pdo_getsum('jy_yxw_tuifee_log',$params,'infee2')?:0;
			}elseif($storetype==2){
				//总部
				unset($params['storeid']);
				$fee=pdo_getsum('jy_yxw_tuifee_log',$params,'infee3');
			}
		}
		$fee -=$fee*16/100;
		$fee=price($fee,'int');
		return $fee;
	}
}
?>